home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / browserMode.tcl < prev    next >
Encoding:
Text File  |  2000-12-05  |  11.2 KB  |  375 lines

  1. #=============================================================================
  2. # Browser mode.
  3. # Alpha cannot do batch searches without this file
  4. #=============================================================================
  5.  
  6. alpha::mode Brws 1.1.1 dummyBrws {} {
  7. } {} help {
  8.     During a batch search, a list of all matching lines are displayed in
  9.     a Brws window.  By using the arrow keys and the return key, you can
  10.     easily jump to the correct file and line of the match you desire.
  11.     See the "Browser-Example" for a demonstration -- click on any
  12.     fileset which appears in the dialog to see a Browser window.
  13.  
  14.     This mode is not intended for text editing.
  15. }
  16.  
  17.  
  18. namespace eval browse {}
  19.  
  20. Bind '\r'    browse::Goto  Brws
  21. Bind enter    browse::Goto  Brws
  22. ascii 0x3      browse::Goto  Brws
  23. Bind down     browse::Down Brws
  24. Bind up     browse::Up   Brws
  25. Bind 'n' <z>    browse::Down Brws
  26. Bind 'p' <z>    browse::Up   Brws
  27. ascii 0x20    browse::Down Brws
  28. ascii 0x8    browse::Up   Brws
  29. # this was below.  do we need it?
  30. Bind 'c' <Cz>    browse::Goto
  31.  
  32. proc dummyBrws {} {}
  33.  
  34. # Set this to 1 to test dynamic code
  35. if {${alpha::platform} == "alpha"} {
  36.     set browse::enableDynamic 0
  37. } else {
  38.     set browse::enableDynamic 1
  39. }
  40.  
  41. proc browse::Up {} {
  42.     set limit [nextLineStart [nextLineStart [minPos]]]
  43.     if {[pos::compare [getPos] > $limit]} {
  44.     set limit [pos::math [getPos] - 1]
  45.     }
  46.     select [lineStart $limit] [nextLineStart $limit]
  47. }
  48.  
  49. proc browse::Down {} {
  50.     set pos [getPos]
  51.     if {[pos::compare $pos < [nextLineStart [minPos]]]} {
  52.     set pos [nextLineStart [minPos]]
  53.     }
  54.     if {[pos::compare [nextLineStart $pos] < [maxPos]]} {
  55.     select [nextLineStart $pos] [nextLineStart [nextLineStart $pos]]
  56.     }
  57. }
  58.  
  59. proc nextPrevMatch {{dir 1} {wname "*Batch Find*"}} {
  60.     set wins [winNames]
  61.     set res [lsearch $wins $wname]
  62.     if {$res < 0} {
  63.     set res [lsearch -regexp $wins {\*.*\*}]
  64.     if {$res < 0} return
  65.     }
  66.     set win [lindex $wins $res]
  67.     bringToFront $win
  68.     if {$dir} {
  69.     browse::Down
  70.     } else {
  71.     browse::Up
  72.     }
  73.     browse::Goto
  74.     dispErr $win
  75. }
  76.  
  77. proc nextMatch {{wname "*Batch Find*"}} {
  78.     nextPrevMatch 1 $wname
  79. }
  80.  
  81. proc prevMatch {{wname "*Batch Find*"}} {
  82.     nextPrevMatch 0 $wname
  83. }
  84.  
  85. proc dispErr {{win "* Compiler Errors *"}} {
  86.     if {[string length $win]} {
  87.     set text [getText -w $win [getPos -w $win] [selEnd -w $win]]
  88.     if {[regexp {(Line.*)∞} $text dummy sub]} {
  89.         message "$sub"
  90.     }
  91.     }
  92. }
  93.         
  94.  
  95. ##############################################################################
  96. #  To be used in the windows created by "matchingLines" or by batch searches.
  97. #
  98. #  With the cursor positioned in a line corrsponding to a match, 
  99. #  go back and select the line in the original file that 
  100. #  generated this match.  (Like emacs 'Occur' functionality)
  101. #
  102. proc browse::Goto {} {
  103.     global browse::GotoProc
  104.     foreach pat [array names browse::GotoProc] {
  105.     if {[string match $pat [win::CurrentTail]]} {
  106.         [set browse::GotoProc($pat)]
  107.         return
  108.     }
  109.     }
  110.     global tileHeight tileWidth tileTop tileLeft tileHeight \
  111.       errorHeight errorDisp tileMargin
  112.     set loc [getPos]
  113.     set ind1 -1
  114.     while {$ind1 < 0} {
  115.     set text [getText [lineStart $loc] [nextLineStart $loc]]
  116.     set ind1 [string first "∞" $text]
  117.     set loc [nextLineStart $loc]
  118.     if {[pos::compare $loc == [maxPos]]} {break}
  119.     }
  120.     set ind2 [string last "∞" $text]
  121.     if {$ind1 == $ind2} {
  122.     set fname [string trim [string range $text $ind1 end] "∞\r\n"]
  123.     set msg ""
  124.     } else {
  125.     set tmp [string trim [string range $text 0 $ind2] "∞\r\n"]
  126.     if {[string last "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t∞" $tmp] < 0} {
  127.         set fname [string trim [string range $text $ind2 end] "∞\r\n"]
  128.         set msg ""
  129.     } else {
  130.         set ind1 [string last "∞" $tmp]
  131.         set fname [string trim [string range $text $ind1 $ind2] "∞\r\n"]
  132.         set msg [string trim [string range $text $ind2 end] "∞\r\n"]
  133.     }
  134.     }
  135.     set loc [getPos]
  136.     set line -1
  137.     while {1} {
  138.     if {[regexp {Line ([0-9]+):} $text "" line]} {break}
  139.     set text [getText [lineStart $loc] [nextLineStart $loc]]
  140.     set loc [pos::math [lineStart $loc] - 1]
  141.     if {[pos::compare $loc <= [minPos]]} {
  142.         # It's a browse window without line numbers, since we've
  143.         # backed up to the top of the window.
  144.         set line -1
  145.         break
  146.     }
  147.     }
  148.     
  149.     set top $tileTop
  150.     set geo [getGeometry]
  151.     if {([lindex $geo 0] != $tileLeft) || ([lindex $geo 1] != $top) \
  152.       || ([lindex $geo 3] != $errorHeight) } {
  153.     moveWin $tileLeft $top
  154.     sizeWin $tileWidth $errorHeight
  155.     }
  156.     set mar $tileMargin
  157.     incr top [expr {$errorHeight + $mar}]
  158.     if {[browse::OpenWindow $fname]} {
  159.     edit -c -w -g $tileLeft $top $tileWidth $errorDisp $fname
  160.     set geo [getGeometry]
  161.     if {([lindex $geo 0] != $tileLeft) || ([lindex $geo 1] != $top) \
  162.       || ([lindex $geo 2] != $tileWidth) || ([lindex $geo 3] != $errorDisp) } {
  163.         sizeWin $tileWidth $errorDisp
  164.         moveWin $tileLeft $top
  165.     }
  166.     } else {
  167.     if {![string match "*Link*" \
  168.       [getText [minPos] [nextLineStart [minPos]]]]} {
  169.         alertnote "File \"$fname\" not found." 
  170.     }
  171.     return
  172.     }
  173.     if {$line >= 0} {
  174.     set pos [rowColToPos $line 0]
  175.     select $pos [nextLineStart $pos]
  176.     }
  177.     message $msg
  178. }
  179.  
  180. proc browse::OpenWindow {fname} {
  181.     global tileHeight tileWidth tileTop tileLeft tileHeight \
  182.       errorHeight errorDisp tileMargin
  183.     if {[file exists $fname]} {
  184.     set top $tileTop
  185.     set mar $tileMargin
  186.     incr top [expr {$errorHeight + $mar}]
  187.     edit -c -w -g $tileLeft $top $tileWidth $errorDisp $fname
  188.     set geo [getGeometry]
  189.     if {([lindex $geo 0] != $tileLeft) || ([lindex $geo 1] != $top) \
  190.       || ([lindex $geo 2] != $tileWidth) || ([lindex $geo 3] != $errorDisp) } {
  191.         sizeWin $tileWidth $errorDisp
  192.         moveWin $tileLeft $top
  193.     }
  194.     return 1
  195.     } else {
  196.     return 0
  197.     }
  198. }
  199.  
  200. set browse::lastMatchingLines ""
  201.  
  202. proc matchingLines {{reg ""} {for 1} {ign 1} {word 0} {regexp 1}} {
  203.     global browse::lastMatchingLines
  204.     
  205.     if {![string length $reg] && \
  206.       [catch {prompt "Regular expression:" [set browse::lastMatchingLines]} reg]} return
  207.     set browse::lastMatchingLines $reg
  208.     if {![string length $reg]} return
  209.     if {!$regexp} {
  210.     set reg [quote::Regfind $reg]
  211.     }
  212.     if {$word} {
  213.     set reg "^.*\\b$reg\\b.*$"
  214.     } else {
  215.     set reg "^.*$reg.*$"
  216.     }
  217.     set pos [expr {$for ? [minPos] : [getPos]}]
  218.     set fileName [win::StripCount [win::Current]]
  219.     set matches 0
  220.     browse::Start {* Matching Lines *} \
  221.       "%d matching lines (<cr> to go to match)\r-----" 
  222.     while {![catch {search -s -f 1 -r 1 -i $ign -- $reg $pos} mtch]} {
  223.     browse::Add $fileName [eval getText $mtch] \
  224.       [lindex [posToRowCol [lindex $mtch 0]] 0] 0
  225.     set pos [lindex $mtch 1]
  226.     incr matches
  227.     }
  228.     browse::Complete
  229. }
  230.  
  231. ## 
  232.  # -------------------------------------------------------------------------
  233.  # 
  234.  # "grepsToWindow" --
  235.  # 
  236.  #  'args' is a list of items
  237.  # -------------------------------------------------------------------------
  238.  ##
  239. proc grepsToWindow {title args} {
  240.     global tileLeft tileTop tileWidth tileHeight errorHeight
  241.     win::SetProportions
  242.     new -n $title -g $tileLeft $tileTop $tileWidth $errorHeight -m Brws \
  243.       -tabsize 8 -info [join $args ""]
  244.     select [nextLineStart [nextLineStart [minPos]]] [nextLineStart [nextLineStart [nextLineStart [minPos]]]]
  245.     message ""
  246. }
  247.  
  248. ## 
  249.  # -------------------------------------------------------------------------
  250.  # 
  251.  # "browse::Format" --
  252.  # 
  253.  #  Can be used by external code to ensure browse information is in an
  254.  #  acceptable format, and to simplify external code.
  255.  # -------------------------------------------------------------------------
  256.  ##
  257. proc browse::Format {file match line {withname 1}} {
  258.     if {$withname} {
  259.     set l [expr {40 - [string length [file tail $file]]}]
  260.     append res "\"[file tail $file]\"; " [format "%$l\s" ""] " "
  261.     } else {
  262.     regsub -all "\t" $match "  " match
  263.     }
  264.     append res [format "Line %d:\r" $line] $match \
  265.       "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t∞$file"
  266.     return $res
  267. }
  268.  
  269. proc browse::RedoCount {} {
  270.     global browse::prefix browse::count
  271.     replaceText [minPos] [pos::math [minPos] + [string length [format [set browse::prefix] 1]]] \
  272.       [format [set browse::prefix] [set browse::count]]
  273. }
  274.  
  275. proc browse::Complete {} {
  276.     global browse::lines browse::none browse::haveWindow browse::count
  277.     if {[string length [set browse::haveWindow]]} {
  278.     bringToFront [set browse::haveWindow]
  279.     browse::RedoCount
  280.     goto [minPos]
  281.     select [nextLineStart [nextLineStart [minPos]]] [nextLineStart [nextLineStart [nextLineStart [minPos]]]]
  282.     setWinInfo read-only 1
  283.     return 0
  284.     } else {
  285.     if {[set browse::count]} {
  286.         browse::createWindow
  287.         setWinInfo read-only 1
  288.         select [nextLineStart [nextLineStart [minPos]]] [nextLineStart [nextLineStart [nextLineStart [minPos]]]]
  289.         return 0
  290.     } else {
  291.         beep
  292.         message [set browse::none]
  293.         return 1
  294.     }
  295.     }
  296. }
  297.  
  298. proc browse::createWindow {} {
  299.     global tileLeft tileTop tileWidth tileHeight errorHeight \
  300.       browse::lines browse::title browse::prefix browse::haveWindow \
  301.       browse::backGround browse::count
  302.     if {[set browse::backGround]} {set w [win::Current]}
  303.     win::SetProportions
  304.     set browse::haveWindow [new -n [set browse::title] \
  305.       -g $tileLeft $tileTop $tileWidth $errorHeight -m Brws \
  306.       -tabsize 8 -shell 1 \
  307.       -text "[format [set browse::prefix] [set browse::count]]\r[join [set browse::lines] \r]\r"]
  308.     set browse::lines {}
  309.     if {[set browse::backGround]} {bringToFront $w}
  310.     message ""
  311. }
  312.  
  313. proc browse::updateWindow {} {
  314.     global browse::haveWindow browse::lines
  315.     placeText -w [set browse::haveWindow] [maxPos -w [set browse::haveWindow]] "[join [set browse::lines] \r]\r"
  316.     set browse::lines {}
  317. }
  318.  
  319. ## 
  320.  # -------------------------------------------------------------------------
  321.  # 
  322.  # "browse::Add" --
  323.  # 
  324.  #  Add the information to our list of browse items.  We can actually 
  325.  #  add these dynamically to the window if we like.
  326.  # -------------------------------------------------------------------------
  327.  ##
  328. proc browse::Add {file match line {withname 1}} {
  329.     global browse::lines browse::dynamic browse::haveWindow browse::count
  330.     lappend browse::lines [browse::Format $file $match $line $withname]
  331.     incr browse::count
  332.     if {[set browse::dynamic]} {
  333.     if {[string length [set browse::haveWindow]]} {
  334.         browse::updateWindow
  335.     } else {
  336.         browse::createWindow
  337.     }
  338.     global alpha::platform
  339.     if {${alpha::platform} != "alpha"} {update}
  340.     }
  341. }
  342.  
  343. ## 
  344.  # -------------------------------------------------------------------------
  345.  # 
  346.  # "browse::Dynamic" --
  347.  # 
  348.  #  Somewhat experimental.
  349.  # -------------------------------------------------------------------------
  350.  ##
  351. proc browse::Dynamic {{backgd 0} {dyn 1}} {
  352.     global browse::dynamic browse::haveWindow browse::backGround browse::enableDynamic
  353.     if {![set browse::enableDynamic]} {return}
  354.     set browse::dynamic $dyn
  355.     set browse::haveWindow ""
  356.     set browse::backGround $backgd
  357. }
  358.  
  359. proc browse::Start {{theTitle {* Matching Lines *}} \
  360.   {thePrefix "%d matching lines (<cr> to go to match)\r-----"} \
  361.   {ifNone "No matches found."}} {
  362.     global browse::lines browse::title browse::prefix browse::none \
  363.       browse::dynamic browse::haveWindow browse::backGround browse::count
  364.     set browse::lines {}
  365.     set browse::title $theTitle
  366.     set browse::prefix $thePrefix
  367.     set browse::none $ifNone
  368.     set browse::dynamic 0
  369.     set browse::haveWindow ""
  370.     set browse::backGround 0
  371.     set browse::count 0
  372. }
  373.  
  374.